package physics;

import physics.Collider.ColliderType;
import math.Vec2;

public class Collision{
	public static boolean IsCollsision(Vec2 normal){
		if(normal.Magnitude() != 0){
			return true;
		}else{
			return false;
		}
	}
	
	public static Vec2 CheckCollision(Collider body1, Collider body2){
		if(body1.type == ColliderType.RECT && body2.type == ColliderType.RECT){
			return AABBCollision(body1, body2);
		}else if(body1.type == ColliderType.CIRCLE && body2.type == ColliderType.CIRCLE){
			//return CircleCollision(body1, body2);
		}else if(body1.type == ColliderType.RECT && body2.type == ColliderType.CIRCLE){
			return AABBCircleCollision(body1, body2);
		}else if(body1.type == ColliderType.CIRCLE && body2.type == ColliderType.RECT){
			return AABBCircleCollision(body2, body1);
		}else{
			return new Vec2();
		}
        return new Vec2();
	}
	
	private static Vec2 AABBCollision(Collider body1, Collider body2){
		Vec2 distance = new Vec2();
		Vec2 absDistance = new Vec2();
		float xMag;
		float yMag;

		distance.x = body1.pos.x - body2.pos.x;
		distance.y = body1.pos.y - body2.pos.y;

		float xAdd = (body1.size.x + body2.size.x) / 2.0f;
		float yAdd = (body1.size.y + body2.size.y) / 2.0f;

		if(distance.x < 0){
			absDistance.x =  distance.x * -1;
		}else{
			absDistance.x = distance.x;
		}

		if(distance.y < 0){
			absDistance.y = distance.y * -1;
		}else{
			absDistance.y = distance.y;
		}

		if(!((absDistance.x < xAdd) && absDistance.y < yAdd)){
			return new Vec2();
		}

		xMag = xAdd - absDistance.x;
		yMag = yAdd - absDistance.y;

		Vec2 normal = new Vec2();
		if(xMag < yMag){
			if(distance.x > 0){
				normal.x = xMag;
			}else{
				normal.x = -xMag;
			}
		}else{
			if(distance.y > 0){
				normal.y = yMag;
			}else{
				normal.y = -yMag;
			}
		}

		return normal;
	}
	
	public static boolean CircleCollision(Collider body1, Collider body2){
        System.out.print("Body1: ");
        System.out.println(body1.pos);
        System.out.print("Body2: ");
        System.out.println(body2.pos);
    
		Vec2 dist = new Vec2();
		dist = body2.pos.Subtract(body1.pos);
        System.out.print("Dist: ");
        System.out.println(dist);
  
        Vec2 absDist = new Vec2();
        absDist = dist.Abs();
        System.out.print("AbsDist: ");
        System.out.println(dist);
		
		float add = absDist.Magnitude() - (body1.radius + body2.radius);
        System.out.println(add);
        System.out.println(absDist.Magnitude());
		
		if(add < 0){
			//return new Vec2();
            return true;
		}
        return false;
		
		/*Vec2 normal = new Vec2();
		normal = dist.Normalise();
		normal = normal.Multiply(add);*/
		
		//return normal;
	}
	
	private static Vec2 AABBCircleCollision(Collider body1, Collider body2){
		return new Vec2();
	}
}
